package com.horizon.mobile.activity;

import java.util.ArrayList;
import java.util.List;

import zuo.biao.library.base.BaseHttpListFragment;
import zuo.biao.library.interfaces.AdapterCallBack;
import zuo.biao.library.interfaces.CacheCallBack;
import zuo.biao.library.manager.HttpManager.OnHttpResponseListener;
import zuo.biao.library.util.JSON;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.horizon.mobile.adapter.TodoAdapter;
import com.horizon.mobile.manage.SharePreferences;
import com.horizon.mobile.model.Result;
import com.horizon.mobile.model.Todo;
import com.horizon.mobile.util.HttpRequest;

public class TodoListFragment extends BaseHttpListFragment<Todo, TodoAdapter>
        implements CacheCallBack<Todo> {
    public static final String ARGUMENT_RANGE = "ARGUMENT_RANGE";
    public int page = 1;
    public List<Todo> listTodo = new ArrayList<Todo>();
    /**
     * 是否创建
     */
    protected boolean isCreate = false;
    protected boolean isHiddem = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        argument = getArguments();
        initCache(this);

        // 功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        // 功能归类分区方法，必须调用>>>>>>>>>>

//        lvBaseList.onRefresh();

        return view;
    }

    @Override
    public List parseArray(String json) {
        // TODO Auto-generated method stub
        return JSON.parseArray(json, Todo.class);
    }

    @Override
    public void getListAsync(int page) {
        this.page = page;
        HttpRequest.getTodoList(SharePreferences.getInstance(context)
                .getToken(), "TodoList", "", page, 10, 0, listener);
    }

    @Override
    public void initData() {// 必须调用
        super.initData();

    }

    @Override
    public Class<Todo> getCacheClass() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getCacheGroup() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getCacheId(Todo data) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getCacheCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void setList(final List<Todo> list) {
        listTodo = list;
        setList(new AdapterCallBack<TodoAdapter>() {

            @Override
            public void refreshAdapter() {
                adapter.refresh(list);

            }

            @Override
            public TodoAdapter createAdapter() {
                // TODO Auto-generated method stub
                return new TodoAdapter(context);
            }
        });

    }

    OnHttpResponseListener listener = new OnHttpResponseListener() {

        @Override
        public void onHttpResponse(int requestCode, String resultJson,
                                   Exception e) {
            boolean isNull = resultJson == null;
            if (resultJson != null) {
                Result result = JSON.parseObject(resultJson, Result.class);
                if (result.isSuccess()) {
                    listTodo = JSON.parseArray(result.getData().toString(),
                            Todo.class);
                    onLoadSucceed(page, listTodo);
                    // setList(list);
                } else {
                    if (result != null)
                        showShortToast(result.getMessage());
                    else
                        showShortToast("连接服务器失败！");
                }
            } else {
                if (e != null)
                    showShortToast(e.getMessage());
                else
                    showShortToast("连接服务器失败！");
            }
            onStopRefresh();
        }

    };

    @Override
    public void initEvent() {// 必须调用
        super.initEvent();

        lvBaseList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                toActivity(FlowDetailActivity.createIntent(context,
                        listTodo.get(position - 1).getWorkId(), listTodo.get(position - 1)
                                .getTitle(), listTodo.get(position - 1).getTrackId(), listTodo.get(position - 1).getSubjectionId()));
            }
        });
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isCreate = true;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isHiddem) {
            lvBaseList.onRefresh();
        }
    }

//	@Override
//	public void setUserVisibleHint(boolean isVisibleToUser) {
//		super.setUserVisibleHint(isVisibleToUser);
//		if (isVisibleToUser && isCreate) {
//			//相当于Fragment的onResume
//			//在这里处理加载数据等操作
//			lvBaseList.onRefresh();
//		} else {
//			//相当于Fragment的onPause
//		}
//	}

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && isCreate) {
            if (lvBaseList != null)
                lvBaseList.onRefresh();
        }
        isHiddem = hidden;
    }
}
